package com.hongshan.wallet.core.wallet

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.databinding.ObservableField
import android.databinding.ViewDataBinding
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.hongshan.wallet.BR
import com.hongshan.wallet.Constant
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPropertyChangedCallback
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.router.RouterIndex
import kotlinx.android.synthetic.main.activity_commission.*
import kotlinx.android.synthetic.main.appbar.*

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.wallet
 * @author   : zhangxx
 * @date     : 2019/3/28
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */

@Route(path = RouterIndex.COMMISSIONS)
class CommissionDescActivity: MyActivity<ViewDataBinding, CommissionViewModel>() {

    val adapter by lazy { CommissionPagerAdapter(this, viewModel.type) }

    override fun initVariableId() = BR.vm

    override fun initViewModel(): CommissionViewModel {
        val viewModel = ViewModelProviders.of(this)[CommissionViewModel::class.java]
        viewModel.type = 4
        return viewModel
    }

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_commission

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tvTitle.text = "佣金明细"
        rg.check(R.id.rbAll)
        vpContent.adapter = adapter
        viewModel.index.addOnPropertyChangedCallback(BaseOnPropertyChangedCallback<ObservableField<Int>>{sender,_->
            val checkId = when(sender.get()){
                0 -> R.id.rbAll
                1 -> R.id.rbOrder
                2 -> R.id.rbTopUp
                else -> R.id.rbWithdrawal
            }
            rg.check(checkId)
        })
        rg.setOnCheckedChangeListener { _, id ->
            when(id){
                R.id.rbAll -> viewModel.index.set(0)
                R.id.rbOrder -> viewModel.index.set(1)
                R.id.rbTopUp -> viewModel.index.set(2)
                R.id.rbWithdrawal -> viewModel.index.set(3)
            }
        }
        refreshData()
        viewModel.refreshData.observe(this, Observer { refreshData() })
    }

    fun refreshData() {
        adapter.vms[viewModel.index.get()!!].refresh(1, Constant.OTHER_CONFIG.ROWS, viewModel.startTime, viewModel.endTime)
    }
}